﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;

using Styx;
using Styx.Helpers;
using Styx.Plugins.PluginClass;
using Styx.WoWInternals;
using System.Drawing;
using System.Threading;

namespace Answer
{
    public delegate void ChangedEventHandler(object sender, EventArgs e);
    
    class Core
    {

        
        private float minpercent = 5f;

        private List<Section> sectionList = new List<Section>();
        public event ChangedEventHandler Changed;
        private XMLReader xml;

        private Color Error = Color.Red;
        private Color Success = Color.Green;
        private Color Working = Color.Orange;

        public Core()
        {
            xml = new XMLReader();
        }
        protected virtual void OnChanged(EventArgs e)
        {
            if (Changed != null)
                Changed(this, e);
        }

        public float getMinpercent()
        {
            return this.minpercent;
        }

        public void setMinpercent(float percent)
        {
            this.minpercent = percent;
        }

        public void Clear()
        {
            Logging.Write(Working, "[AutoAnswer] - Clearing Sectionlist");
            sectionList.Clear();
            Logging.Write(Success, "[AutoAnswer] - Sectionlist cleared");
        }

       
        public bool Load()
        {
            try
            {

                //DirectoryInfo di = new DirectoryInfo(Application.StartupPath + "\\Settings\\Include");
                DirectoryInfo di = new DirectoryInfo(Application.StartupPath + "\\Plugins\\AutoAnswer\\Settings\\Include");
                FileInfo[] rgFiles = di.GetFiles("*.xml");

                foreach (FileInfo fi in rgFiles)
                {
                    
                    Logging.Write(Working, "[AutoAnswer] - Loading file \"" + fi.Name + "\"");
                    xml.Open(fi);
                    while (xml.ContainsSection())
                    {

                        sectionList.Add(xml.CreateSection());

                    }
                    xml.Close();

                    Logging.Write(Success, "[AutoAnswer] - \"" + fi.Name + "\" loaded");
                }

                Logging.Write(Success, "[AutoAnswer] - Files loaded.");
                return true;
            }
            catch (Exception ex)
            {
                Logging.Write(Error, "[AutoAnswer] - Error while Loading! " + ex.Message);
                return false;
            }

        }

        public float Calculate(string FirstString, string SecondString)
        {
            try
            {

                string[] first = FirstString.ToUpper().Split(' ');
                string[] second = SecondString.ToUpper().Split(' ');

                int Length = 0;
                float matchweight = 0f;
                float matchpercent = 0f;

                if (first.Length <= second.Length)
                    Length = second.Length;
                else
                    Length = first.Length;


                if (Length > 0)
                    matchweight = 100f / Length;

                string[] firstString = new string[Length];
                string[] secondString = new string[Length];

                for (int x = 0; x < firstString.Length; x++)
                {
                    firstString[x] = " ";
                }

                for (int x = 0; x < secondString.Length; x++)
                {
                    secondString[x] = " ";
                }

                for (int x = 0; x < first.Length; x++)
                {
                    firstString[x] = first[x];
                }

                for (int x = 0; x < second.Length; x++)
                {
                    secondString[x] = second[x];
                }


                for (int x = 0; x < firstString.Length; x++)
                {
                    for (int y = 0; y < secondString.Length; y++)
                    {
                        if (firstString[x].Equals(secondString[y]))
                        {
                            matchpercent += matchweight * 1;
                        }
                    }
                }


                return matchpercent;
            }
            catch (Exception ex)
            {

                Logging.Write(Error, "[AutoAnswer] - Error Calculate percent!");
                return 0f;
            }
        }

        private void ExecuteTransaction(string partner, string message, bool ignore)
        {

        }

        public void GetAnswer(string message, string partner)
        {
            Dictionary<int, float> answerlist = new Dictionary<int, float>();

            float percent = 0f;
            for (int i = 0; i < sectionList.Count; i++)
            {
                if (sectionList[i].getSectionId() == -1)
                    continue;

                percent = Calculate(message, sectionList[i].getMessage());

                if (percent > minpercent)
                    answerlist.Add(sectionList[i].getSectionId(), percent);
            }

            var items = from pair in answerlist
                        orderby pair.Value descending
                        select pair;

            // Display results.
            int selected = -1;
            foreach (KeyValuePair<int, float> pair in items)
            {
                selected = pair.Key;
                break;
            }

            
            answerlist.Clear();

            Section value;
            if (selected == -1)
            {
                value = sectionList[0];
            }
            else
            {
                value = sectionList[selected];
            }

            Dictionary<int, string> ret = value.getAnswer(partner);

            foreach (KeyValuePair<int, string> pair in ret)
            {
                Worker work = new Worker(partner, pair.Value, value.PutOnIgnore(partner), pair.Key);
                Thread workerThread = new Thread(work.Execute);
                workerThread.Start();
            }

        }
    }
}
